package pkg

import (
	"219yk/appconfig"
	"context"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

const BUCKET_NAME = "wmzzg3"

// 定义全局变量
func Upload(localFile string, fileName string) {
	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider(appconfig.AppConfig.Upload.AccessKeyID, appconfig.AppConfig.Upload.AccessKeySecret)).
		WithRegion("cn-shanghai")

	// 创建OSS客户端
	client := oss.NewClient(cfg)

	// 创建上传管理器
	u := client.NewUploader()

	// 定义本地文件路径，需要替换为您的实际本地文件路径和文件名

	// 执行上传大文件的操作
	result, err := u.UploadFile(context.TODO(),
		&oss.PutObjectRequest{
			Bucket: oss.Ptr(BUCKET_NAME),
			Key:    oss.Ptr(fileName),
			Acl:    oss.ObjectACLPublicRead},
		localFile)
	if err != nil {
		log.Fatalf("failed to upload file %v", err)
	}

	// 打印上传文件的结果
	log.Printf("upload file result:%#v\n", result)
}

func Urlt(filename string) string {
	sprintf := fmt.Sprintf("https://%s.oss-cn-shanghai.aliyuncs.com/%s", BUCKET_NAME, filename)
	return sprintf
}
